#include "testwidget.h"
#include "ui_testwidget.h"

TestWidget::TestWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("测试");
    initWidget();
    m_item = new QTreeWidgetItem(newTree);
}

TestWidget::~TestWidget()
{
    delete ui;
}
void TestWidget::initWidget()
{
    newTree = new QTreeWidget(this);
    newTree->setStyleSheet("QTreeWidget{background-color:rgb(64,64,64);color:rgb(255,255,255);}\
                            QHeaderView::section{background-color:rgb(64,64,64);color:rgb(255,255,255);\
                            border:none;font:14pt}");
    newTree->headerItem()->setText(0,QString());
    newTree->setGeometry(0,0,this->width(),this->height());
    QStringList headers;
    headers<<"类型"<<"描述"<<"字节类型"<<"序号"<<"字节"<<"位"<<"值";
    newTree->setHeaderLabels(headers);
    QTreeWidgetItem *group[GlobalConfig::recvMVBList.length()];
    for(int i=0;i<GlobalConfig::recvMVBList.length();++i)
    {
        group[i] = new QTreeWidgetItem(newTree);
        if(GlobalConfig::recvMVBList.at(i).port>0&&GlobalConfig::recvMVBList.at(i).port<16)
            group[i]->setText(0,"0x00"+QString().setNum(GlobalConfig::recvMVBList.at(i).port,16).toUpper());
        else if(GlobalConfig::recvMVBList.at(i).port>=16&&GlobalConfig::recvMVBList.at(i).port<255)
            group[i]->setText(0,"0x0"+QString().setNum(GlobalConfig::recvMVBList.at(i).port,16).toUpper());
        else
            group[i]->setText(0,"0x"+QString().setNum(GlobalConfig::recvMVBList.at(i).port,16).toUpper());
    }
    for(int i=0;i<GlobalConfig::recvMVBList.length();++i)
    {
        for(int j=0;j<GlobalConfig::recvMVBList[i].node.length();++j)
        {
            QTreeWidgetItem *item = new QTreeWidgetItem(group[i]);
            item->setText(0,"");
            item->setText(1,GlobalConfig::recvMVBList[i].node.at(j).description);
            switch (GlobalConfig::recvMVBList[i].node.at(j).size) {
            case 1:
                item->setText(2,"BOOLEAN1");
                break;
            case 8:
                item->setText(2,"UNSIGNED8");
                break;
            case 16:
                item->setText(2,"UNSIGNED16");
                break;
            case 32:
                item->setText(2,"UNSIGNED32");
                break;
            default:
                break;
            }
            item->setText(3,GlobalConfig::recvMVBList[i].node.at(j).index);
            item->setText(4,QString().setNum(GlobalConfig::recvMVBList[i].node.at(j).byte));
            item->setText(5,QString().setNum(GlobalConfig::recvMVBList[i].node.at(j).bit));
            item->setText(6,"0");
        }
    }
    connect(newTree,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(slot_valueChange(QTreeWidgetItem*,int)));
    connect(newTree,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(slot_itemDoubleClicked(QTreeWidgetItem*,int)));
    connect(newTree,SIGNAL(itemSelectionChanged()),this,SLOT(slot_itemSelectionChanged()));
}
void TestWidget::slot_valueChange(QTreeWidgetItem* item,int column)
{
    int index=0;
    int port =0;
    port = item->parent()->text(0).toUInt(NULL,16);
    index = item->text(4).toInt()*10+item->text(5).toInt();
    if(column == 6)
    {
        qDebug()<<"value change is"<<item->text(column);
        qDebug()<<"ComidCount change is"<<port<<"IndexCount change is"<<index;
        emit valueChange(port,index,item->text(column).toULong());
    }

}
void TestWidget::slot_itemDoubleClicked(QTreeWidgetItem *item, int column)
{

    if(column == 6)
    {
        qDebug()<<"column"<<column<<"is double clicked";
        newTree->openPersistentEditor(item,column);
        m_item = item;
        m_column = column;
    }

}
void TestWidget::slot_itemSelectionChanged()
{
    if(m_item!=NULL)
        newTree->closePersistentEditor(m_item,m_column);
}
